package com.example

/**
 * 学习循环
 * Created by Walkud on 17/8/21.
 */
object ForWhile {

    @JvmStatic
    fun main(array: Array<String>) {

        forTest()
        whileTest()
        doWhileTest()
    }

    /**
     * 学习For
     */
    fun forTest() {

        println("Begin forTest")

        val items = getItems()

        //遍历Item 自动识别类型
        for (item in items) {
            println("item:$item")
        }

        println()


        //遍历Item 循环体(代码块)
        for (item: String in items) {
            println("item:$item")
        }

        println()

        //遍历下标
        for (index in items.indices) {
            println("item:${items[index]}")
        }

        println()

        //遍历下标,不包含为0的下标
        for (index in items.indices - 0) {
            println("item:${items[index]}")
        }

        println()


        //同时遍历下标及Value，使用函数库withIndex
        for ((index, value) in items.withIndex()) {
            println("item index:$index,value:$value")
        }

        println()

        //闭区间，从0到items.size - 1
        for (index in 0 until items.size - 1) {
            println("item index:$index")
        }

        println("End forTest")
        println()
    }

    /**
     * 学习While
     */
    fun whileTest() {
        println("Begin whileTest")

        val items = getItems()

        var index = 0
        while (index < items.size) {

            println("item:${items[index]}")

            index++
        }

        println("End whileTest")
        println()

    }

    /**
     * 学习DoWhile
     */
    fun doWhileTest() {
        println("Begin doWhileTest")

        val items = getItems()
        var index = 0

        do {
            println("item:${items[index]}")
        } while (++index < items.size)

        println("End doWhileTest")
        println()
    }

    fun getItems(): ArrayList<String> {
        return arrayListOf("Android", "Gradle", "Kotlin")
    }
}